﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections;

/// <summary>
/// Summary description for class_treeview
/// </summary>
public class class_treeview
{
    class_database db;
    public string table = "tbl_loaimau";
    public string nodeName = "name";
    public string nodeValue = "id";
    ArrayList listChildNode = new ArrayList();
    ArrayList listChildMauBaiViet = new ArrayList();

    public class_treeview()
    {
    }

    public string getMaxGroupID()
    {
        db = new class_database();
        string result = Convert.ToString(db.GetValue(table, "MAX(id)"));
        return result;
    }


    public string getGroupNameByID(string group_id)
    {
        group_id = class_string.clear_inject(group_id);
        db = new class_database();
        string _result = Convert.ToString(db.GetValue(table, nodeName, "ID ='" + group_id + "'"));
        return _result;
    }

    public string getGroupNameByMemberName(string member_name)
    {
        db = new class_database();
        member_name = class_string.clear_inject(member_name);
        string sql = "SELECT GroupName FROM group_tb WHERE ID =(SELECT Group_ID FROM `member_tb` WHERE MemberName='"+member_name+"' LIMIT 1)";
        DataTable dt = db.GetDataTable(sql);
        if (dt.Rows.Count > 0)
        {
            string group_name = Convert.ToString(dt.Rows[0]["GroupName"]);
            return group_name;
        }
        else
        {
            return null;
        }
    }

    public DataTable getTreeData()
    {
        db = new class_database();
        DataTable dt = db.GetDatatableFromDbOrCache("SELECT id, name FROM " + table + " ORDER BY name", "loaiMauCache");
        return dt;
    }

    public DataTable getAllTreeView()
    {
        db = new class_database();
        DataTable dt = db.GetDataTable("SELECT * FROM " + table);
        return dt;
    }
    public DataTable getTreeNodeData(string parentID)
    {
        db = new class_database();
        string sql = "SELECT id, name FROM tbl_mau WHERE type = " + parentID;
        DataTable dt = db.GetDataTable(sql);
        return dt;
    }
    public bool insertNode(string node_name, string parent_id)
    {
        db = new class_database();
        string[] _param = new string[] { nodeName, nodeValue };
        string[] _value = new string[] { node_name, parent_id };
        string[] _type = new string[] { "string", "int" };

        if (db.InsertRecord(table, _param, _value, _type))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool editNode(string new_name, string node_id)
    {
        db = new class_database();
        string[] _param = new string[] { nodeName };
        string[] _value = new string[] { new_name };
        string[] _type = new string[] { "string" };
        string _where = "id='" + node_id + "'";
        if (db.UpdateRecord(table, _param, _value, _where))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool deleteNode(string node_id)
    {
        db = new class_database();
        node_id = class_string.clear_inject(node_id);
        if (db.DeleteRecord(table, "id='" + node_id + "'"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public DataTable getNodeData(string node_id)
    {
        db = new class_database();
        node_id = class_string.clear_inject(node_id);
        String sql = "SELECT * FROM " + table + " WHERE id ='" + node_id + "'";
        DataTable dt = db.GetDataTable(sql);

        if (dt != null && dt.Rows.Count > 0)
        {
            return dt;
        }
        else
        {
            return null;
        }
    }

    public DataTable getNodeDataWithArrayNodeID(string _array_node_id)
    {
        db = new class_database();
        _array_node_id = class_string.clear_inject(_array_node_id);
        String sql = "SELECT * FROM " + table + " WHERE id IN (" + _array_node_id + ") ORDER BY GroupName ASC";
        DataTable dt = db.GetDataTable(sql);

        if (dt != null && dt.Rows.Count > 0)
        {
            return dt;
        }
        else
        {
            return null;
        }
    }


    public Boolean changeGroupToGroupWithParentGroupID(string group_id, string parent_group_id)
    {
        group_id = class_string.clear_inject(group_id);
        parent_group_id = class_string.clear_inject(parent_group_id);

        db = new class_database();

        string[] _param = { "Parent_ID" };
        string[] _value = { parent_group_id };
        if (db.UpdateRecord(table, _param, _value, " ID='" + group_id + "'"))
            return true;
        else
            return false;
    }

    public DataTable getLastChildNodes()
    {
        db = new class_database();
        DataTable dt = db.GetDataTable("SELECT * FROM " + table + " WHERE id NOT IN (SELECT Parent_ID FROM group_tb) ORDER BY GroupName ASC");
        return dt;

    }
    public string getAllChildNodeStringWithParentID(string _parent_id)
    {
        ArrayList _result = this.getAllChildNodeWithParentID(_parent_id);
        string _where_sql = "";
        int count = 0;
        foreach (string id_value in _result)
        {
            if (count == (_result.Count - 1))
                _where_sql += id_value;
            else
                _where_sql += id_value + ",";

            count++;
        }
        return _where_sql;
    }

    public ArrayList getAllChildNodeWithParentID(string node_id)
    {
        node_id = class_string.clear_inject(node_id);

        //Liet ke danh sach cac nut con
        db = new class_database();
        string sql2 = "SELECT id FROM group_tb WHERE parent_id ='" + node_id + "'";
        DataTable dt2 = db.GetDataTable(sql2);

        if (dt2 != null && dt2.Rows.Count > 0)
        {
            int count2 = 0;
            while (count2 < dt2.Rows.Count)
            {
                listChildNode.Add(Convert.ToString(dt2.Rows[count2]["id"]));
                getAllChildNodeWithParentID(Convert.ToString(dt2.Rows[count2]["id"]));
                count2++;
            }
        }
        return listChildNode;
    }

    public string getAllChildMemberStringWithParentID(string _group_str)
    {
        ArrayList _result = this.getAllChildMemberWithParentID(_group_str);
        string _where_sql = "";
        int count = 0;
        foreach (string id_value in _result)
        {
            if (count == (_result.Count - 1))
                _where_sql += id_value;
            else
                _where_sql += id_value + ",";

            count++;
        }
        return _where_sql;
    }
    public ArrayList getAllChildMemberWithParentID(string _group_str)
    {
        _group_str = class_string.clear_inject(_group_str);
        //Liet ke danh sach cac nut con
        db = new class_database();
        string sql2 = "SELECT id FROM member_tb WHERE Group_ID IN(" + _group_str + ")";
        DataTable dt2 = db.GetDataTable(sql2);

        if (dt2 != null && dt2.Rows.Count > 0)
        {
            int count2 = 0;
            while (count2 < dt2.Rows.Count)
            {
                listChildMauBaiViet.Add(Convert.ToString(dt2.Rows[count2]["id"]));
                count2++;
            }
        }
        return listChildMauBaiViet;
    }

}